package com.nchu.learn.netty.resolver;

import com.nchu.learn.netty.enums.MessageTypeEnum;
import com.nchu.learn.netty.model.Message;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author fujianjian
 * @project self-learning
 * @date 2020/1/16 11:33
 */
@Slf4j
public class ResponseMessageResolver implements Resolver {
    private final static AtomicInteger counter = new AtomicInteger(1);

    @Override
    public Message resolve(Message message) {
        int index = counter.getAndIncrement();
        log.info("[trx: {}] {} .receive response:{}", message.getSessionId(), index, message.getBody());
        log.info("[trx: {}] {} .attachments:{}", message.getSessionId(), index, message.getAttachments());

        // 生成返回消息，由于是响应所以生成一个Empty消息返回
        Message empty = new Message();
        empty.setMessageType(MessageTypeEnum.EMPTY);
        return empty;
    }

    @Override
    public boolean support(Message message) {
        return message.getMessageType() == MessageTypeEnum.RESPONSE;
    }

}
